<template>
  <a-modal
    title="编辑二手车车源"
    :width="900"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleSubmit"
    @cancel="handleCancel"
  >
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
        <a-form-item v-show="false"><a-input v-decorator="['id']" /></a-form-item>
        <a-form-item
          label="车源编号"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback
        >
          <a-input placeholder="请输入车源编号" v-decorator="['sourceNum', {rules: [{required: true, message: '请输入车源编号！'}]}]" />
        </a-form-item>
        <a-form-item
          label="车型id"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback
        >
          <a-input placeholder="请输入车型id" v-decorator="['modelId', {rules: [{required: true, message: '请输入车型id！'}]}]" />
        </a-form-item>
        <a-form-item
          label="指导价"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback
        >
        </a-form-item>
        <a-form-item
          label="销售价格"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback
        >
        </a-form-item>
        <a-form-item
          label="上牌时间"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback
        >
          <a-input placeholder="请输入上牌时间" v-decorator="['registrationTime', {rules: [{required: true, message: '请输入上牌时间！'}]}]" />
        </a-form-item>
        <a-form-item
          label="表显里程（万公里）"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback
        >
          <a-input placeholder="请输入表显里程（万公里）" v-decorator="['outsideMile', {rules: [{required: true, message: '请输入表显里程（万公里）！'}]}]" />
        </a-form-item>
        <a-form-item
          label="车牌所在省"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback
        >
          <a-input placeholder="请输入车牌所在省" v-decorator="['licenseProvince', {rules: [{required: true, message: '请输入车牌所在省！'}]}]" />
        </a-form-item>
        <a-form-item
          label="车牌所在市"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback
        >
          <a-input placeholder="请输入车牌所在市" v-decorator="['licenseCity', {rules: [{required: true, message: '请输入车牌所在市！'}]}]" />
        </a-form-item>
        <a-form-item
          label="过户次数"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback
        >
          <a-input placeholder="请输入过户次数" v-decorator="['transferNum', {rules: [{required: true, message: '请输入过户次数！'}]}]" />
        </a-form-item>
        <a-form-item
          label="外观"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback
        >
          <a-input placeholder="请输入外观" v-decorator="['outColor', {rules: [{required: true, message: '请输入外观！'}]}]" />
        </a-form-item>
        <a-form-item
          label="内饰"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback
        >
          <a-input placeholder="请输入内饰" v-decorator="['inColor', {rules: [{required: true, message: '请输入内饰！'}]}]" />
        </a-form-item>
        <a-form-item
          label="亮点配置"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback
        >
          <a-input placeholder="请输入亮点配置" v-decorator="['highlightsConfiguration', {rules: [{required: true, message: '请输入亮点配置！'}]}]" />
        </a-form-item>
        <a-form-item
          label="钥匙数量"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback
        >
          <a-input placeholder="请输入钥匙数量" v-decorator="['keyNum', {rules: [{required: true, message: '请输入钥匙数量！'}]}]" />
        </a-form-item>
        <a-form-item
          label="年检到期时间"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback
        >
          <a-input placeholder="请输入年检到期时间" v-decorator="['inspectAnnuallyTime', {rules: [{required: true, message: '请输入年检到期时间！'}]}]" />
        </a-form-item>
        <a-form-item
          label="定金金币数量"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback
        >
          <a-input placeholder="请输入定金金币数量" v-decorator="['depositPrice', {rules: [{required: true, message: '请输入定金金币数量！'}]}]" />
        </a-form-item>
        <a-form-item
          label="0:待上架 1:已上架"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback
        >
          <a-input placeholder="请输入0:待上架 1:已上架" v-decorator="['status', {rules: [{required: true, message: '请输入0:待上架 1:已上架！'}]}]" />
        </a-form-item>
        <a-form-item
          label="0未删除 1已删除"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback
        >
          <a-input placeholder="请输入0未删除 1已删除" v-decorator="['deleteFlag', {rules: [{required: true, message: '请输入0未删除 1已删除！'}]}]" />
        </a-form-item>
        <a-form-item
          label=""
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback
        >
          <a-input placeholder="请输入" v-decorator="['createBy', {rules: [{required: true, message: '请输入！'}]}]" />
        </a-form-item>
        <a-form-item
          label=""
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback
        >
          <a-input placeholder="请输入" v-decorator="['updateBy', {rules: [{required: true, message: '请输入！'}]}]" />
        </a-form-item>
        <a-form-item
          label=""
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback
        >
          <a-input placeholder="请输入" v-decorator="['remark', {rules: [{required: true, message: '请输入！'}]}]" />
        </a-form-item>
      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
  import { carSourceOldEdit } from '@/api/modular/main/carsourceold/carSourceOldManage'
  export default {
    data () {
      return {
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 }
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 15 }
        },
        visible: false,
        confirmLoading: false,
        form: this.$form.createForm(this)
      }
    },
    methods: {
      // 初始化方法
      edit (record) {
        this.visible = true
        setTimeout(() => {
          this.form.setFieldsValue(
            {
              id: record.id,
              sourceNum: record.sourceNum,
              modelId: record.modelId,
              guidePrice: record.guidePrice,
              salePrice: record.salePrice,
              registrationTime: record.registrationTime,
              outsideMile: record.outsideMile,
              licenseProvince: record.licenseProvince,
              licenseCity: record.licenseCity,
              transferNum: record.transferNum,
              outColor: record.outColor,
              inColor: record.inColor,
              highlightsConfiguration: record.highlightsConfiguration,
              keyNum: record.keyNum,
              inspectAnnuallyTime: record.inspectAnnuallyTime,
              depositPrice: record.depositPrice,
              status: record.status,
              deleteFlag: record.deleteFlag,
              createBy: record.createBy,
              updateBy: record.updateBy,
              remark: record.remark
            }
          )
        }, 100)
      },
      handleSubmit () {
        const { form: { validateFields } } = this
        this.confirmLoading = true
        validateFields((errors, values) => {
          if (!errors) {
            for (const key in values) {
              if (typeof (values[key]) === 'object' && values[key] != null) {
                values[key] = JSON.stringify(values[key])
              }
            }
            carSourceOldEdit(values).then((res) => {
              if (res.success) {
                this.$message.success('编辑成功')
                this.confirmLoading = false
                this.$emit('ok', values)
                this.handleCancel()
              } else {
                this.$message.error('编辑失败')//  + res.message
              }
            }).finally((res) => {
              this.confirmLoading = false
            })
          } else {
            this.confirmLoading = false
          }
        })
      },
      handleCancel () {
        this.form.resetFields()
        this.visible = false
      }
    }
  }
</script>
